var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        page : {
            isend : false,
            doing : false,
            doo : 'sort_v1',
            pdata : {
                op : 'search',
                page : 1,
                key : '',
                stype : 'goods',
                shopid : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        list : [],
        inited : false,
        history : [],
    },
    onLoad:function(options){
        var self = this;
        self.setData({
            options : options,
            'page.pdata.stype' : options.type ? options.type : 'goods',
            'page.pdata.shopid' : options.shopid ? options.shopid : 0,
        });
        
        app.com.getLocation(self,function(loc){
            var postdata = {
                op:'info',
                lat : loc.latitude,
                lng : loc.longitude,
                shopid : options.shopid ? options.shopid : 0,
            }
            app.com.http('search','POST',postdata,0,false,'','',function(res){
                if (!res.data.errno && res.data) {
                    self.setData({
                        inited : true,
                        params : res.data.data.params,
                        history : res.data.data.history ? res.data.data.history : [],
                        mylike : res.data.data.mylike ? res.data.data.mylike : [],
                    });
                }
            });
        })

        wx.setNavigationBarTitle({
            title:  '搜索',
        });
    },
    comfirm(e){
        var self = this;
        if( !e.detail || e.detail == '' ) return false;
        this.setData({
            list : [],
            'page.pdata.key' : e.detail,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
        })
        app.com.auth(function(){
            app.com.getPage(self,self.data.page,20,function(res){
                var data = res.data.data;
                self.setData({
                    list : res.data.data.list,
                });
            },false,function(res){
                app.com.alert(res.data.message);
            });
        });
    },
    onShareAppMessage : function(){
        return false;
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.isdown = true;
        this.onLoad( this.data.options );
        wx.stopPullDownRefresh();
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                list : self.data.list.concat(res.data.data.list)
            });
        });
    },
    searchhist(e){
        var self = this;
        var tar = {
            detail : e.currentTarget.dataset.key
        };
        this.comfirm(tar);
    },
    falsesearch(){
        this.setData({
            'page.pdata.key' : '',
            list : [],

        });
    },
    tabChange(e){
        this.setData({
            list : [],
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0,
            'page.pdata.stype' : e.currentTarget.dataset.type
        })
        var tar = {
            detail : this.data.page.pdata.key,
        }
        this.comfirm(tar);
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e,this.data.tid );
    },
    redirectto : function( e ){
        app.com.redirectto( e,this.data.tid );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    showkefuimg : function(e){
        app.com.alert('长按图片识别二维码联系客服',function(){
            wx.previewImage({
                current: e.currentTarget.dataset.img,
                urls: [e.currentTarget.dataset.img]
            });
        });
    },
    copy(e){
        wx.setClipboardData({
            data : e.currentTarget.dataset.copy
        })
    }

})